package com.example.myFirstWebmagic.util;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * ClassName: UrlUtil 
 * @Description: 工具类
 * @author xiongyao
 * @date 2016-12-21
 */
public class UrlUtil {
	public static boolean isChineseChar(String str){
       boolean temp = false;
       Pattern p=Pattern.compile("[\u4e00-\u9fa5]"); 
       Matcher m=p.matcher(str);
       if(m.find()){
           temp =  true;
       }
       return temp;
	}
	public static String encodeUrl(String url,String enc) throws UnsupportedEncodingException{
		URI uri=URI.create(url.replace(" ", "%20"));
		String queryPart=uri.getQuery();
		Map<String, String> paramsMap=new HashMap<String, String>();
		List<String> paramsList =new ArrayList<String>();
		paramsList=Arrays.asList(queryPart.split("&"));
		for(String params:paramsList){
			String key=params.split("=")[0];
			String value=params.split("=")[1];
			if(isChineseChar(value)){
				value=URLEncoder.encode(value,enc);
			}
			paramsMap.put(key,value);
		}
		paramsList =new ArrayList<String>();
		for(Entry<String, String> entry:paramsMap.entrySet()){
			paramsList.add(entry.getKey()+"="+entry.getValue());
		}
		return "http://"+uri.getHost()+uri.getPath()+"?"+StringUtils.join(paramsList, "&");
	}
	
	/**
	 * 
	 * @Description: 获取Url中某个参数的值
	 * @param @param url
	 * @param @param paraName
	 * @param @return   
	 * @return String  
	 * @throws
	 * @author xiongyao
	 * @date 2017年3月5日
	 */
	public static String getUrlParamValue(String url,String paraName){
		URI uri=URI.create(url.replace(" ", "%20"));
		String queryPart=uri.getQuery();
		Map<String, String> paramsMap=new HashMap<String, String>();
		List<String> paramsList =new ArrayList<String>();
		paramsList=Arrays.asList(queryPart.split("&"));
		for(String params:paramsList){
			String key=params.split("=")[0];
			String value=params.split("=")[1];
			paramsMap.put(key,value);
		}
		return paramsMap.get(paraName);
	}
	
	/**
	 * 
	 * @Description: 替换url中的某个参数的值
	 * @param @param url
	 * @param @param targetParaName
	 * @param @param value
	 * @param @return   
	 * @return String  
	 * @throws
	 * @author xiongyao
	 * @date 2017年3月5日
	 */
	public static String replaceUrlParamValue(String url,String targetParaName,String replaceValue){
		URI uri=URI.create(url.replace(" ", "%20"));
		String queryPart=uri.getQuery();
		Map<String, String> paramsMap=new HashMap<String, String>();
		List<String> paramsList =new ArrayList<String>();
		paramsList=Arrays.asList(queryPart.split("&"));
		for(String params:paramsList){
			String key=params.split("=")[0];
			String value=params.split("=")[1];
			paramsMap.put(key,value);
		}
		paramsMap.put(targetParaName, replaceValue);
		paramsList =new ArrayList<String>();
		for(Entry<String, String> entry:paramsMap.entrySet()){
			paramsList.add(entry.getKey()+"="+entry.getValue());
		}
		return "http://"+uri.getHost()+uri.getPath()+"?"+StringUtils.join(paramsList, "&");
	}
	
	public static void main(String[] args) {
		//System.out.println(UrlUtil.getUrlParam("http://datacenter.mep.gov.cn/index!environmentAirHourDetail.action?cityCode=140300&searchTime=2017-03-05%2006:00:00", "searchTime"));
		System.out.println(UrlUtil.replaceUrlParamValue("http://datacenter.mep.gov.cn/index!environmentAirHourDetail.action?cityCode=140300&searchTime=2017-03-05%2006:00:00", "searchTime","2017-03-01%2006:00:00"));
	}
	
	public static String getHost(String url){
		URI uri=URI.create(url);
		return "http://"+uri.getHost()+uri.getPath();
	}
}
